package wfm.activiti;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;

import com.constants.Constants;

public class ActivitiManager {

	private static ProcessEngine processEngine = null;
	private RuntimeService runtimeService = null;
	private TaskService taskService = null;
	private RepositoryService repositoryService=null;
	private FormService formService=null;
	private HistoryService historyService=null;

	private static ActivitiManager instance = null;
	
	private Map<String, String> taskErrorMap = new HashMap<String, String>();

	public static ActivitiManager getInstance() {
		if (instance == null) {
			instance = new ActivitiManager();
		}

		return instance;
	}

	public Deployment deployProcess(String name) {
		startEnginesAndServices();
		
		System.out.println("Deployed Process");

		// Deploy the process definition
		return repositoryService.createDeployment().addClasspathResource(name)
				.deploy();
	}
	
	public void startEnginesAndServices(){
		processEngine = ProcessEngineConfiguration
				.createStandaloneProcessEngineConfiguration()
				.buildProcessEngine();
		
		repositoryService = processEngine
				.getRepositoryService();
		runtimeService = processEngine.getRuntimeService();
		
		taskService = processEngine.getTaskService();
		
		formService=processEngine.getFormService();
		
		historyService=processEngine.getHistoryService();
		
	}

	public String startProcess() {
		startEnginesAndServices();
		
		// Start a process instance
		String procId = runtimeService.startProcessInstanceByKey(Constants.PROCESS_NAME).getId();

		System.out.println("Started Process");

		return procId;
	}

	public ProcessEngine getProcessEngine() {
		return processEngine;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public TaskService getTaskRuntimeService() {
		return taskService;
	}
	
	public RepositoryService getRepositoryService() {
		return repositoryService;
	}
	
	public static boolean isInstanciated() {
		return (instance != null);
	}
	
	public String getTaskError(String executionId) 
	{ 
		return this.getTaskError(executionId, true);
	}
	
	public String getTaskError(String executionId, boolean removeError) 
	{ 
		String taskError = this.taskErrorMap.get(executionId);
		if (removeError)
			this.taskErrorMap.put(executionId, ""); 
		return taskError; 
	}

	public void setTaskError(String executionId, String taskError) 
	{ 
		// Check if the error contains HTML tags
		if (taskError.startsWith("<"))
		{
			// remove opening tag - <h3>
			taskError = taskError.substring(taskError.indexOf(">")+1);
			// remove closing tag - </h3>
			taskError = taskError.substring(0, taskError.lastIndexOf("</"));
		}
		this.taskErrorMap.put(executionId, taskError);
	}

	public FormService getFormService() {
		return formService;
	}

	public HistoryService getHistoryService() {
		return historyService;
	}

	
	
}
